package com.bms.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.bms.utils.JdbcUtil;
import com.bms.utils.SqlExpressUtil;

public abstract class SimpleAbstractDao implements ISimpleDao {
	/* (non-Javadoc)
	 * @see com.bms.dao.IDao#getSimpleBeanListById(java.lang.Class, int)
	 */
	public <T> List<T> getSimpleBeanListById(Class<T> clazz, int... ids) throws SQLException {
		List<T> list = new ArrayList<T>();
		try {
			for (int id : ids) {
				list.add(this.getBeanById(id, clazz));
			}
		} catch (Exception e) {
			throw new SQLException();
		}
		return list;
	}
	/* (non-Javadoc)
	 * @see com.bms.dao.IDao#updateSimpleBeansById(java.util.List)
	 */
	public int updateSimpleBeansById(List<Object> beanList) throws SQLException{
		int rows=0;
		try {
			JdbcUtil.beginTransaction();
			for (Object bean : beanList) {
				this.updateBeanById(bean);
				rows=rows+1;
			}
		} catch (SQLException e) {
			try {
				JdbcUtil.rollbackTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
				throw e;
			}
			e.printStackTrace();
			throw e;
		}finally{
			JdbcUtil.commitTransaction();
			JdbcUtil.close();
		}
		return rows;
	}
	/* (non-Javadoc)
	 * @see com.bms.dao.IDao#insertSimpleBeansById(java.util.List)
	 */
	public int insertSimpleBeansById(List<Object> beanList) throws SQLException{
		int rows=0;
		try {
			JdbcUtil.beginTransaction();
			for (Object bean : beanList) {
				this.insertBean(bean);
				rows=rows+1;
			}
		} catch (SQLException e) {
			try {
				JdbcUtil.rollbackTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
				throw e;
			}
			e.printStackTrace();
			throw e;
		}finally{
			JdbcUtil.commitTransaction();
			JdbcUtil.close();
		}
		return rows;
	}
	/* (non-Javadoc)
	 * @see com.bms.dao.IDao#deleteSimpleBeansById(java.util.Map)
	 */
	public int deleteSimpleBeansById(Map<Integer,Class> map) throws SQLException{
		int rows=0;
		try {
			JdbcUtil.beginTransaction();
			for (Entry<Integer, Class> entry : map.entrySet()) {
				this.deleteById(entry.getKey(),entry.getValue());
				rows=rows+1;
			}
		} catch (SQLException e) {
			try {
				JdbcUtil.rollbackTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
				throw e;
			}
			e.printStackTrace();
			throw e;
		}finally{
			JdbcUtil.commitTransaction();
			JdbcUtil.close();
		}
		return rows;
	}
	/* (non-Javadoc)
	 * @see com.bms.dao.IDao#getBeanById(int, java.lang.Class)
	 */
	public <T> T getBeanById(int id, Class<T> clazz) throws SQLException {
		T obj = null;
		QueryRunner runner = null;
		String sql = null;
		try {
			obj = clazz.newInstance();
			runner = new QueryRunner(JdbcUtil.getDataSource());
			sql = SqlExpressUtil.getSelectExprById(clazz);
			obj = (T) runner.query(sql, new BeanHandler(clazz), new Object[]{id});
			System.out.println(sql);
		} catch (Exception e) {
			throw new SQLException();
		}
		return obj;
	}
	/* (non-Javadoc)
	 * @see com.bms.dao.IDao#updateBeanById(java.lang.Object)
	 */
	public void updateBeanById(Object bean) throws SQLException {
		Map<String,Object> map = null;
		QueryRunner runner = null;
		try {
			runner = new QueryRunner();
			map = SqlExpressUtil.getUpdateExprById(bean);
			runner.update(JdbcUtil.getConnection(), map.get("sql").toString(), (Object[]) map.get("objArray"));
		} catch (SQLException e) {
			throw e;
		}
	}
		
	/* (non-Javadoc)
	 * @see com.bms.dao.IDao#insertBean(java.lang.Object)
	 */
	public void insertBean(Object bean) throws SQLException{
		Map<String,Object> map = null;
		QueryRunner runner = null;
		try{
			runner = new QueryRunner();
			map = SqlExpressUtil.getInsertExpr(bean);
			runner.update(JdbcUtil.getConnection(), map.get("sql").toString(), (Object[]) map.get("objArray"));
		}catch(SQLException e){
			throw e;
		}
	}
	/* (non-Javadoc)
	 * @see com.bms.dao.IDao#deleteById(int, java.lang.Class)
	 */
	public void deleteById(int id,Class clazz) throws SQLException{
		Map<String,Object> map = null;
		QueryRunner runner = null;
		try{
			runner = new QueryRunner();
			map = SqlExpressUtil.getDeleteExpr(id,clazz);
			runner.update(JdbcUtil.getConnection(), map.get("sql").toString(), (Object[]) map.get("objArray"));
		}catch(SQLException e){
			throw e;
		}
	}
}
